Imports UWPCS3340

Public Class frmProg7

   Private userList As New ArrayList
   Private user As New UserAccount
   Private transactionTotal As Short = 0
   Private balance As Short = 1000
   Private subtotal As Short = 0

   Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click

      user = New UserAccount
      user.mainForm = Me
      user.DoTransaction = AddressOf doTrans
      user.Report = AddressOf doReport
      userList.Add(user)
      user.SpinUp()
      txtLogScroll()

   End Sub

   Private Sub cmdWait_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWait.Click

      If Not lstUsers.SelectedIndex = -1 Then
         userList(lstUsers.SelectedIndex).UserStop()
      End If

   End Sub

   Private Sub cmdResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdResume.Click

      If Not lstUsers.SelectedIndex = -1 Then
         userList(lstUsers.SelectedIndex).UserContinue()
      End If

   End Sub

   Private Sub cmdTerminate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTerminate.Click

      If Not lstUsers.SelectedIndex = -1 Then
         userList(lstUsers.SelectedIndex).SpinDown()
      End If

   End Sub

   Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

      For i As Short = 0 To userList.Count - 1
         userList(i).SpinDown()
      Next

      If MsgBox("Do you want to exit", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
         Application.Exit()
      End If

   End Sub

   Private Sub doTrans(ByVal ID As String, ByVal value As Integer, ByVal Final As Boolean)

      If Final Then
         lstUsers.Items.Item(findUser(ID)) = "User " & ID & ": Terminated Subtotal: " & value
      Else
         txtLog.Text &= "User " & ID & ": " & value & vbCrLf
         balance += value
         transactionTotal += value
         subtotal = value
      End If

      updateNumbers()

      txtLogScroll()

   End Sub

   Private Sub doReport(ByVal ID As String, ByVal theState As UserState)

      Dim text As String = "User " & ID & ": " & theState.ToString

      Try

         If theState = UserState.Started Then

            lstUsers.Items.Add(text)
            txtLog.Text &= text & vbCrLf

         ElseIf theState = UserState.Terminating Then

            txtLog.Text &= text & vbCrLf

         ElseIf theState = UserState.Terminated Then

            txtLog.Text &= text & vbCrLf
            updateNumbers()
            txtLogScroll()

         Else

            lstUsers.Items.Item(findUser(ID)) = text

         End If

      Catch ex As Exception

         MsgBox(ex.Message)
         Console.WriteLine(ex.Message)

      End Try
      

   End Sub

   Private Function findUser(ByVal id As String) As Integer

      For x As Integer = 0 To userList.Count
         If CType(userList(x), UserAccount).ID = id Then
            Return userList.IndexOf(CType(userList(x), UserAccount))
         End If
      Next

   End Function

   Private Sub txtLogScroll()

      txtLog.SelectionStart = txtLog.Text.Length
      txtLog.ScrollToCaret()

   End Sub

   Private Sub frmProg7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      updateNumbers()

   End Sub

   Private Sub updateNumbers()

      txtBalance.Text = Format(balance, "c")
      txtTransactionTotal.Text = Format(transactionTotal, "c")

   End Sub
End Class
